package com.jinmdz.fmis.api.quartz;

import com.jinmdz.fmis.core.util.LogUtil;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定时触发器
 *
 * @author LiCongLu
 * @date 2019-12-23 09:12
 */
@Configuration
public class GlobalTrigger {

    /**
     * 定时是否开启
     */
    @Value("${quartz.startup:false}")
    private boolean startup;

    /**
     * 定时触发器cron
     */
    @Value("${quartz.global.cron-expression}")
    private String cronExpression;

    /**
     * 定义定时工作详情
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2019/7/9 9:11
     */
    @Bean
    public JobDetail jobDetail() {
        return JobBuilder
                .newJob(GlobalJob.class)
                .withIdentity("globalJob")
                .storeDurably().build();
    }

    /**
     * 定义定时触发器
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2019/7/9 9:11
     */
    @Bean
    public Trigger cronTrigger() {

        LogUtil.info("准备触发器");
        if (!startup) {
            LogUtil.info("未开启触发器");
            return null;
        }

        LogUtil.info("已开启，定时cron:" + cronExpression);

        return TriggerBuilder.newTrigger()
                .forJob(jobDetail())
                .withIdentity("globalTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
                .build();
    }
}
